package com.qc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.BannerDao;
import com.qc.entity.BannerEntity;
import com.qc.entity.UserEntityVo;

@Repository("bannerDao")
public class BannerDaoImpl implements BannerDao{

    @Resource(name="jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public int getListBannerCount(String name) {

        String sql = "select count(*) from t_banner where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public List getAllBanner(String name, int pageNo, int pageSize) {

        int beginRow = (pageNo - 1)* pageSize;
        String sql = "select id,name,imgUrl,position,count from t_banner where 1 = 1 and if_del = '0'  ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public int saveBanner(BannerEntity bannerEntity) {
        
        String sql = "insert into t_banner(name,position,parameter,level,type,count,imgUrl,open) values(?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,new Object[]{
                                    bannerEntity.getName(),bannerEntity.getPosition(),
                                    bannerEntity.getParameter(),bannerEntity.getLevel(),
                                    bannerEntity.getType(),bannerEntity.getCount(),
                                    bannerEntity.getImgUrl(),bannerEntity.getOpen()});
    }

    @Override
    public int deleteBannerId(long id) {

        String sql = "update t_banner set if_del = '1' where id = " +id;
        return jdbcTemplate.update(sql);
    }

    @Override
    public int updateBannerId(BannerEntity bannerEntity) {

        String sql = "update t_banner set name = ? ,position= ? ,parameter= ? ,level= ? ,type= ? ,imgUrl= ? ,open= ?   where id = ? ";
        return jdbcTemplate.update(sql,new Object[]{
                bannerEntity.getName(),bannerEntity.getPosition(),
                bannerEntity.getParameter(),bannerEntity.getLevel(),
                bannerEntity.getType(),bannerEntity.getImgUrl(),
                bannerEntity.getOpen(),bannerEntity.getId()});
    }

    @Override
    public BannerEntity getBannerEntity(String id) {
        String sql = " select id,name,position,parameter,level,type,imgUrl,open from t_banner where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(id)) {
            sql += " and id = " + id;
        }
        
        BannerEntity bannerEntity = new BannerEntity();
        jdbcTemplate.query(sql, new RowMapper<BannerEntity>() {
            public BannerEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
                
                bannerEntity.setName(rs.getString("name"));
                bannerEntity.setParameter(rs.getString("parameter"));
                bannerEntity.setPosition(rs.getInt("position"));
                bannerEntity.setLevel(rs.getInt("level"));
                bannerEntity.setType(rs.getInt("type"));
                bannerEntity.setImgUrl(rs.getString("imgUrl"));
                bannerEntity.setOpen(rs.getInt("open"));
                bannerEntity.setId(rs.getLong("id"));
                
                return bannerEntity;
            }
        });
        return bannerEntity;
    }

}
